package team.yidong.fenda.utils;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


/**
 * Created by xuanlong on 2017/10/9.
 */
@Component
public class QiNiuYunUtil {
    //设置好账号的ACCESS_KEY和SECRET_KEY
    private String ACCESS_KEY = "WwJHARNNGD1Q9Z6mprSlxofJMCulCUOsaZFgzXjN"; //这两个登录七牛 账号里面可以找到
    private String SECRET_KEY = "XSDUidA9wXM8k3anB_lmvL-4ITMthuIBhqZ5nLXy";

    //要上传的空间
    private String bucketName = "liuxlfile"; //对应要上传到七牛上 你的那个路径（自己建文件夹 注意设置公开）

    //创建上传对象
    UploadManager uploadManager = new UploadManager();

    public String getUpToken() {
        //密钥配置
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        StringMap policy = new StringMap();

        policy.putNotEmpty("returnBody",
                "{\"key\":\"$(key)\"}");
        //获取token
        String upToken = auth.uploadToken(bucketName);
        return upToken;
    }

    public void uploadFiles(byte[] bytes, String key) {
        try {
            Response res = uploadManager.put(bytes, key, getUpToken());
        } catch (QiniuException e) {
            e.printStackTrace();
        }
    }
}
